package net.wanho.index.controller;

import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.systemSettings.po.Employee;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.service.EmployeeService;
import net.wanho.systemSettings.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/LoginServlet")
public class LoginServlet extends BaseServlet<EmployeeService> {
	private MenuService menuService = BeanFactory.getBean(MenuService.class) ;
	protected AjaxResult loginCheck(HttpServletRequest request, HttpServletResponse response){
		String employeeId = request.getParameter("employeeId");
		String password = request.getParameter("password");
		try {
			//根据工号查询
			Employee employee = super.baseService.findById(employeeId);
			//工号不存在 或者密码错误
			// 【修改调用 md5工具类  进行加密 !employee.getLogIn().getEmmPassword().equals(Md5Util.md5(password)) 】
			if (employee==null || !employee.getLogIn().getEmmPassword().equals(password)){
				return AjaxResult.fail("工号或密码错误！") ;
			}
			// 状态禁用
			if (employee.getStatus().equals("0")){
				return AjaxResult.fail("账号锁定！") ;
			}
			// 是否关联职位
			if (employee.getPosition().getPositionId()==0){
				return  AjaxResult.fail("您没有权限访问任何资源");
			}
			//登录成功
			//设置当前会话中 绑定用户
			request.getSession().setAttribute("currEmployee",employee);
			return AjaxResult.success() ;
		} catch (ServiceException e) {
			e.printStackTrace();
			return AjaxResult.fail("登录异常") ;
		}
	}
	
	protected String loginToCms(HttpServletRequest request,HttpServletResponse response){
		//根据当前已经登录的用户 查询所能够看到的菜单
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("currEmployee");
		//获得当前登录用户的职位
		int positionId = employee.getPosition().getPositionId();
		//调用 MenuService的方法 进行查询
		Map<String,String> condMap = new HashMap<>(1) ;
		condMap.put("positionId",String.valueOf(positionId)) ;
		//获得当前职位关联的菜单集合
		List<Menu> menuList = menuService.findByCond(condMap);
		//当前职位关联的菜单集合 【父子关系匹配成功后的】
		employee.getPosition().setMenuList(menuList);
		return "redirect:/ControlServlet?method=countAll" ;
	}
	protected void updatePassword(HttpServletRequest request,HttpServletResponse response){
		JSONObject json = new JSONObject();
			json.put("status", "1");
			json.put("msg", "修改成功");
		try {
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void logout(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/login.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
